package com.yzmoney.wechat.action;

import java.util.HashMap;
import java.util.Map;

import com.yzmoney.common.constant.Constants;
import com.yzmoney.common.model.JsonResult;
import com.yzmoney.common.service.StaticService;
import com.yzmoney.wechat.service.DepartmentService;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 企业号通讯录中的用户操作
 * @author DOVE
 * 
 */
@Controller
@RequestMapping("/department")
public class DepartmentAction
{
	private static Logger logger = Logger.getLogger(DepartmentAction.class);

	@Resource
	private DepartmentService departmentService;
	@Resource
	private StaticService staticService;
	
	/**
	 * 通讯录创建用户
	 * @param request
	 * @param response
	 * @param tpMsg 创建用户json格式数据
	 * @return json格式代码
	 */
	@RequestMapping("/create")
	@ResponseBody
	public JsonResult create(HttpServletRequest request, HttpServletResponse response)
	{
		logger.info(Constants.LOGPRIFIX + "---create begin");
		Map model = new HashMap();
		String tpMsg = request.getParameter("tpMsg");
		JsonResult jsonResult = new JsonResult();
		if (StringUtils.isNotEmpty(tpMsg))
		{
			logger.info(Constants.LOGPRIFIX + "----tpMsg=" + tpMsg);
			jsonResult = departmentService.departmentOperate(tpMsg,"create");
			return jsonResult;
		}
		jsonResult.setCode(Constants.CODE_FAILURE);
		jsonResult.setMsg("请传入有效的模版");
		return jsonResult;
	}
	/**
	 * 修改通讯录用户
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/update")
	@ResponseBody
	public JsonResult updae(HttpServletRequest request, HttpServletResponse response)
	{
		logger.info(Constants.LOGPRIFIX + "---update begin");
		Map model = new HashMap();
		String tpMsg = request.getParameter("tpMsg");
		JsonResult jsonResult = new JsonResult();
		if (StringUtils.isNotEmpty(tpMsg))
		{
			logger.info(Constants.LOGPRIFIX + "----tpMsg=" + tpMsg);
			jsonResult = departmentService.departmentOperate(tpMsg,"update");
			return jsonResult;
		}
		jsonResult.setCode(Constants.CODE_FAILURE);
		jsonResult.setMsg("请传入有效的模版");
		return jsonResult;
	}
	/**
	 * 删除通讯录用户
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public JsonResult delete(HttpServletRequest request, HttpServletResponse response)
	{
		logger.info(Constants.LOGPRIFIX + "---delete begin");
		String userId = request.getParameter("id");
		JsonResult jsonResult = new JsonResult();
		if (StringUtils.isNotEmpty(userId))
		{
			logger.info(Constants.LOGPRIFIX + "----userId=" + userId);
			jsonResult = departmentService.userDelete(userId,"delete");
			return jsonResult;
		}
		jsonResult.setCode(Constants.CODE_FAILURE);
		jsonResult.setMsg("请传入有效的模版");
		return jsonResult;
	}
}
